package com.example.asus.duihuakuang;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.btn_dialog1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showSimpleDialog();
            }
        });
        findViewById(R.id.btn_dialog2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showChoiceDialog();
            }
        });
        findViewById(R.id.btn_dialog3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showSingleDialog();
            }
        });
        findViewById(R.id.btn_dialog4).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showCheckDialog();
            }
        });
        findViewById(R.id.btn_dialog5).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showzidingyiDialog();
            }
        });
    }
    /*自定义对话框*/
    private void showzidingyiDialog() {
        /*定义一个输入框*/
        final EditText editText=new EditText(MainActivity.this);

        View view= LayoutInflater.from(MainActivity.this)
                .inflate(R.layout.dialog_input,null,false);
        final EditText nameEdit=view.findViewById(R.id.edt_name);
        final EditText ageEdit=view.findViewById(R.id.edt_age);

        new AlertDialog.Builder(MainActivity.this)
                .setIcon(R.mipmap.ic_launcher)
                .setTitle("请输入您的姓名以及年龄")
                .setView(view)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                       String name=nameEdit.getText().toString();
                       int age=Integer.parseInt(ageEdit.getText().toString());
                        Toast.makeText(MainActivity.this,
                                "您的姓名是"+name+" 年龄是"+age,Toast.LENGTH_SHORT).show();
                    }
                })


                .create()
                .show();
    }

    /*复选对话框*/
    private void showCheckDialog() {
        final String[] hobbies={"吃鸡","王者","睡觉","听歌","打代码"};
        final boolean ischecked[]=new boolean[5];
        new AlertDialog.Builder(MainActivity.this)
                .setIcon(R.mipmap.ic_launcher)
                .setTitle("请选择您的爱好")
                .setMultiChoiceItems(hobbies, null, new DialogInterface.OnMultiChoiceClickListener() {

                    public void onClick(DialogInterface dialogInterface, int i, boolean b) {
                        //保存每次点击后的值
                        ischecked[i]=b;
                        if(b){
                            Toast.makeText(MainActivity.this,
                                    "您添加了"+hobbies[i],Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(MainActivity.this,
                                    "您取消了"+hobbies[i],Toast.LENGTH_SHORT).show();
                        }
                    }
                })
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        StringBuffer buffer=new StringBuffer();

                        for(int j=0;j<ischecked.length;j++){
                            if(ischecked[j]){
                                buffer.append(hobbies[j]);
                            }
                        }
                        Toast.makeText(MainActivity.this,
                                "您的爱好有"+buffer,Toast.LENGTH_SHORT).show();
                    }
                })
                .create()
                .show();
    }

    /*单选对话框*/
    private void showSingleDialog() {
        final String[] SEX={"男","女","不知道"};
        new AlertDialog.Builder(MainActivity.this)
                .setIcon(R.mipmap.ic_launcher)
                .setTitle("请选择您的性别")
                .setSingleChoiceItems(SEX, -1, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(MainActivity.this,
                                "您的性别是"+SEX[i],Toast.LENGTH_SHORT).show();
                    }
                })
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                })
                .create()
                .show();
    }
    /*选择对话框*/
    private void showChoiceDialog() {
        final String [] cities={"广州","深圳","北京","天津","南京"};
        new AlertDialog.Builder(MainActivity.this)
                .setIcon(R.mipmap.ic_launcher)
                .setTitle("请选择您的城市")
                .setItems(cities, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(MainActivity.this,
                                "您居住的城市是"+cities[i],Toast.LENGTH_SHORT).show();
                    }
                })
                .create()
                .show();
    }
    /* 简单对话框*/
    private void showSimpleDialog() {
        new AlertDialog.Builder(MainActivity.this)
                .setIcon(R.mipmap.ic_launcher)
                .setTitle("提示")
                .setMessage("今天是你的生日")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(MainActivity.this,
                                "谢谢",Toast.LENGTH_SHORT).show();
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this,
                                "今天不是我生日",Toast.LENGTH_SHORT).show();
                    }
                })
                .setNeutralButton("无所谓", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this,
                                "无所谓",Toast.LENGTH_SHORT).show();
                    }
                })
                .create()
                .show();
    }

}
